import {
  Column,
  Entity,
  OneToOne,
  PrimaryGeneratedColumn,
  JoinColumn,
  Unique,
  ManyToMany,
  JoinTable,
  BeforeInsert,
  ManyToOne,
} from 'typeorm';
import { Profile } from './profile.entity';
import { Exclude } from 'class-transformer';
import { Roles } from '../../roles/entities/role.entity';
import { Friend } from '../../friend/entities/friend.entity';
import { qqCode } from '../../../utils/qqCode';
@Entity()
export class User {
  @PrimaryGeneratedColumn()
  id: number;

  @Column()
  @Unique(['username'])
  username: string;

  @Column()
  @Exclude()
  password: string;

  @Column({ default: null })
  code: string;

  @OneToOne(() => Profile, (profile) => profile.user)
  @JoinColumn()
  profile: Profile;

  @ManyToMany(() => Roles, (role) => role.users)
  @JoinTable({ name: 'user_roles' })
  roles: Roles[];

  @ManyToOne(() => Friend, (friend) => friend.friend)
  @JoinColumn({ referencedColumnName: 'id' })
  friend: Friend;

  @BeforeInsert()
  BeforeInsert() {
    this.code = qqCode();
  }
}
